package org.jeecg.modules.business.third.strategy.emsdms;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.dock.entity.mds.MdsBaseResponseVO;
import org.jeecg.modules.dock.entity.mds.request.MDSLaminationAndRefeedingRequestVO;
import org.jeecg.modules.dock.entity.mes.request.LaminationReturnRequestVO;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.service.MdsDockingRepeaterService;
import org.jeecg.modules.flow.sys.constant.FlowConstant;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    叠片退料(柱料) 21 dms
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r21DmsWarehouse")
@RequiredArgsConstructor
@Slf4j
public class R21DmsWarehouse implements ThirdOperateStrategy {

    private final IThirdCommonService thirdCommonService;
    private final MdsDockingRepeaterService mdsDockingRepeaterService;


    @Override
    public boolean operate(Map<String, Object> map) {
        AtomicBoolean result = new AtomicBoolean(true);
        try {
            Optional<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialsOptional = thirdCommonService.getWmsInReceiptTaskMaterials(map.get("taskId").toString());
            if (wmsInReceiptTaskMaterialsOptional.isPresent()) {
                WmsInReceiptTaskMaterial taskMaterial = wmsInReceiptTaskMaterialsOptional.get();

                LaminationReturnRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText(), LaminationReturnRequestVO.class);
                MDSLaminationAndRefeedingRequestVO request = new MDSLaminationAndRefeedingRequestVO();
                request.setTaskCode(vo.getTaskCode());
                request.setOperateTime(new Date());
                request.setTaskType(vo.getTaskType());
                request.setTaskStatus(MdsStatusEnum.TASK_RECEIVED.getCode());
                request.setSite(vo.getSite());
                request.setOperatorName(vo.getOperatorName());
                request.setLineName(vo.getLineName());
                request.setOrderCode(vo.getOrderCode());
                if(ObjectUtil.isNotNull(vo.getDetail())){
                    List details = vo.getDetail().stream().map(o -> (Object) o).collect(Collectors.toList());
                    request.setDetail(details);
                }
                MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.mdsLaminationAndRefeeding(request);
                if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                    if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            } else {
                result.set(false);
            }
        } catch (Exception e) {
            log.error("R21DmsWarehouse:异常，{}", e.getMessage(), e);
            result.set(false);
        }
        return result.get();
    }
}
